package com.bsoft.gol.hcs.specialist.dao;


import com.bsoft.gol.hcs.specialist.entity.FollowUpRecordsEducation;
import com.google.common.collect.Lists;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.atag.dao.ext.HibernateExtendDAO;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.specialist.entity.FollowUpRecordsContent;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

public abstract class FollowUpRecordsContentDAO extends HibernateExtendDAO<FollowUpRecordsContent> {

	@DAOMethod(sql = " from FollowUpRecordsContent where recordId in (:recordIds) ")
	public abstract List<FollowUpRecordsContent> queryByRecordIds(@DAOParam("recordIds") List<Long> recordIds);

	@DAOMethod(sql = " from FollowUpRecordsContent where recordId =:recordId ")
	public abstract List<FollowUpRecordsContent> queryContentByRecordId(@DAOParam("recordId") Long recordId);

	@DAOMethod(sql = " select count(1) from FollowUpRecordsContent where recordId =:recordId and educationStatus =0 ")
	public abstract Long getCountContentByRecordId(@DAOParam("recordId") Long recordId);

	@DAOMethod(sql = " update FollowUpRecordsContent set educationStatus=1,educationTime=:educationTime where recordId =:recordId and educationId =:educationId ")
	public abstract void updateEducationStatus(@DAOParam("recordId") Long recordId, @DAOParam("educationId") String educationId, @DAOParam("educationTime") Date educationTime);

	/**
	 * 根据随访计划id查询健康宣教
	 *
	 * @param recordId 随访计划id
	 * @return 健康宣教
	 */
	public List<FollowUpRecordsEducation> queryFollowUpContentById(final Long recordId) {
		HibernateStatelessResultAction<List<FollowUpRecordsEducation>> action = new AbstractHibernateStatelessResultAction<List<FollowUpRecordsEducation>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer sql = new StringBuffer();
				sql.append(" select c.education_id as educationId ,c.education_name as educationName ,c.education_status as educationStatus,c.education_time as educationTime from follow_up_records_education c where c.record_id=:recordId ");
				Query query = ss.createSQLQuery(sql.toString());
				query.setParameter("recordId", recordId);

				query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
				List<Map<String, Object>> list = query.list();
				List<FollowUpRecordsEducation> contentList = Lists.newArrayList();
				if (CommonUtils.isNotEmpty(list)) {
					for (Map<String, Object> map : list){
                        FollowUpRecordsEducation content = ConversionUtils.convert(map, FollowUpRecordsEducation.class);
						contentList.add(content);
					}
				}
				setResult(contentList);
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}
}
